var express = require('express');
var router = express.Router();
var sqlQuery = require('../../lcMsql')

/* GET users listing. */
router.get('/', async function(req, res, next) {
    let page = req.query.page;
    page=page?page:1
    let sqlStr = 'select * from user limit ?,5'
    let result = await sqlQuery(sqlStr,[(parseInt(page) - 1)*5])
    let options = {
          userlist:result
    }
  res.render('admin/users/userlist1',options);
});

router.post('/deluser', async function(req,res){
    let dellist = req.body['dellist[]']
    console.log(req.body,dellist)
    dellist.forEach(async item => {
        let sqlStr = 'delete * from user where id = ?'
        await sqlQuery(sqlStr,item)
    });
    res.json({
        stack:'ok',
        content:"删除成功"
    })
})

router.get('/api/userlist',async (req,res)=>{
    let page = req.query.page;
    let limitNum = req.query.limit;
    let sqlStr = 'select `user`.id,`user`.username,`user`.mail,`user`.roleid,role.rolename from user LEFT JOIN role on `user`.roleid = role.id';
    
    let arr = [(page-1)*limitNum,limitNum]
    let result = await sqlQuery(sqlStr,arr)
    let options = {
        "code":0,
        "msg":'',
        "count": 1000,
        "data": Array.from(result)
    }
    res.json(options)
})



module.exports = router;
